﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace lxwmvc
{
    /// <summary>
    /// 分页信息
    /// </summary>
    public class pagerinfo
    {
        /// <summary>
        /// 得到每页数量
        /// </summary>
        private int pageSize = 20;
        /// <summary>
        /// 当前页码
        /// </summary>
        private int currentPage = 1;

        /// <summary>
        /// 每页记录数量
        /// </summary>
        public int pagesize
        {
            get { return pageSize; }
            set
            {
                if (value > 0)
                    pageSize = value;
            }
        }

        /// <summary>
        /// 获取和设置当前第几页
        /// </summary>
        public int currentpage
        {
            get { return currentPage; }
            set
            {
                if (value <= 0)
                    currentPage = 1;
                else if (value > pagecount)
                    currentPage = pagecount;
                else
                    currentPage = value;
            }
        }

        /// <summary>
        /// 记录前一页
        /// </summary>
        public int pre
        {
            get
            {
                return Math.Max(1, currentPage - 1);
            }
        }

        /// <summary>
        /// 记录后一页
        /// </summary>
        public int next
        {
            get
            {
                return Math.Min(pagecount, currentPage + 1);
            }
        }

        /// <summary>
        /// 总共多少页
        /// </summary>
        public int pagecount
        {
            get
            {
                if (recordcount % pageSize == 0)
                    return recordcount / (int)pageSize;

                return recordcount / (int)pageSize + 1;
            }
        }

        /// <summary>
        /// 数据库记录总数
        /// </summary>
        public int recordcount
        {
            get;
            set;
        }

        /// <summary>
        /// 当前页多少条记录，最后一页不一定就是PageSize
        /// </summary>
        public int currentsize
        {
            get
            {
                if (data != null)
                    return data.Count();
                else
                    return 0;
            }
        }

        /// <summary>
        /// 设置第一条记录的索引
        /// </summary>
        /// <param name="intcurpage">当前页</param> 
        /// <param name="intpagesize">每页大小</param>
        public void setfirstindex(int intcurpage, int intpagesize)
        {
            pagesize = intpagesize;
            currentpage = intcurpage;
        }
        /// <summary>
        /// 得到第一条记录的索引
        /// </summary>
        public int firstindex
        {
            get
            {
                return Math.Max(0, (currentPage - 1) * (int)pageSize);
            }
        }

        /// <summary>
        /// 得到第一条记录的索引
        /// </summary>
        /// <param name="intcurpage">当前页</param>
        /// <param name="intpagesize">每页大小</param>
        /// <returns></returns>
        public int getfirstindex(int intcurpage, int intpagesize)
        {
            pagesize = intpagesize;
            currentpage = intcurpage;

            return firstindex;
        }

        IEnumerable<dynamic> _data = null;
        /// <summary>
        /// 获取分页后的数据
        /// </summary>
        public IEnumerable<dynamic> data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }

        

        /// <summary>
        /// 释放
        /// </summary>
        ~pagerinfo()
        {
            data = null;
        }

        //public string show(string pagesearch,bool lxw)
        //{
        //    int n = 10;

        //    string PageStr = "<font color='#C0C0C0'>每页<b>" + pagesize + "</b>条</font>/<font color='#C0C0C0'>共<b>" + this.recordcount + "</b>条</font>&nbsp;&nbsp;";

        //    int p;
        //    if ((currentpage - 1) % n == 0)
        //        p = (currentpage - 1) / n;
        //    else
        //        p = (((currentpage - 1) - (currentpage - 1) % n) / n);

        //    if (currentpage != 1)
        //        PageStr += "<a href='?page=1&" + pagesearch + "'><font style='font-family:Webdings' color='cdcdcd'>首页</font></a> ";
        //    else
        //        PageStr += "<a><font style='font-family:Webdings' color='cdcdcd'>首页</font></a>";

        //    if (p * n > 0)
        //        PageStr += "<a href='?page=" + (p * n) + "&" + pagesearch + "'><font style='font-family:Webdings' color='0099ff'>首页</font></a> ";
        //    else
        //        PageStr += "<a><font style='font-family:Webdings' color='cdcdcd'>上一页</font></a>";

        //    PageStr += "&nbsp;&nbsp;";

        //    int i = 0;
        //    for (i = p * n + 1; i < p * n + n + 1 && pagecount > 0; i++)
        //    {
        //        if (i == currentpage)
        //            PageStr += "<a style='color:red'>"+i+"</a> ";
        //        else
        //            PageStr += "<a href='?page="+i+"&" + pagesearch + "'>" + i + "</a> ";
        //        if (i == pagecount) break;
        //    }

        //    PageStr += "&nbsp;&nbsp;";

        //    if (i < pagecount)
        //        PageStr += "<a href='?page=" + i + "&" + pagesearch + "'><font style='font-family:Webdings' color='0099ff'>下一页</font></a> ";
        //    else
        //        PageStr += "<a href='?page=" + i + "&" + pagesearch + "'><font style='font-family:Webdings' color='cdcdcd'>下一页</font></a> ";

        //    if (currentpage < pagecount)
        //        PageStr += "<a href='?page=" + pagecount + "&" + pagesearch + "'><font style='font-family:Webdings' color='cdcdcd'>尾页</font></a> ";
        //    else
        //        PageStr += "<font style='font-family:Webdings' color='cdcdcd'>尾页</font> ";

        //    PageStr += "&nbsp;&nbsp;<font color='#C0C0C0'>共<b>" + pagecount + "</b>页</font>";

        //    return PageStr;
        //}
        //public string show(string pagesearch)
        //{
        //    string PageStr = "";
        //    if (currentpage == pagecount && currentpage == 1)
        //    {
        //        //PageStr = "<span>" + currentpage + " / " + pagecount + "</span>";
        //        PageStr = "";
        //    }
        //    else if (currentpage == 1)
        //    {
        //        PageStr = "<span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=txtpage type=\"text\" value='" + currentpage + "' /><a class=\"links\" onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";

        //        //PageStr = "<a href=\"#\"><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=textpage type=\"text\" value=\"\"/><a class=\"links\" href='?page=document.getElementById('textpage').value&" + pagesearch + "'\" >跳转</a>";
        //    }
        //    else if (currentpage == pagecount)
        //    {
        //        PageStr = "<a href='?page=" + (this.currentpage - 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><input id=txtpage type=\"text\" value='" + currentpage + "'/><a class=\"links\"  onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";
        //    }
        //    else
        //    {
        //        PageStr = "<a href='?page=" + (this.currentpage - 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=txtpage type=\"text\" value='" + currentpage + "'/><a class=\"links\"  onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";
        //    }

        //    return PageStr;
        //}
        //public string showpages(string pagesearch)
        //{
        //    //string PageStr = "";
        //    //if (currentpage == 1)
        //    //{
        //    //    PageStr = "<a href=\"#\"><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=txtpage type=\"text\" value='" + currentpage + "' /><a class=\"links\" onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";

        //    //    //PageStr = "<a href=\"#\"><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=textpage type=\"text\" value=\"\"/><a class=\"links\" href='?page=document.getElementById('textpage').value&" + pagesearch + "'\" >跳转</a>";
        //    //}
        //    //else if (currentpage == pagecount)
        //    //{
        //    //    PageStr = "<a href='?page=" + (this.currentpage - 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='#'><i class=\"fa fa-caret-right\"></i></a><input id=txtpage type=\"text\" value='" + currentpage + "'/><a class=\"links\"  onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";
        //    //}
        //    //else
        //    //{
        //    //    PageStr = "<a href='?page=" + (this.currentpage - 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-left\"></i></a><span>" + currentpage + " / " + pagecount + "</span><a href='?page=" + (this.currentpage + 1).tostr() + "&" + pagesearch + "'><i class=\"fa fa-caret-right\"></i></a><input id=txtpage type=\"text\" value='" + currentpage + "'/><a class=\"links\"  onclick=\"javascript:var txtpage =document.getElementById('txtpage').value;  window.location.href='?page='+txtpage+'&" + pagesearch + "'\">跳转</a>";
        //    //}

        //    return show(pagesearch);
        //}

        /// <summary>
        /// 数据json对象
        /// </summary>
        /// <returns></returns>
        public object tojson()
        {
            return new
            {
                pagesize = pagesize,
                currentpage = currentpage,
                pre = pre,
                next = next,
                pagecount = pagecount,
                recordcount = recordcount,
                currentsize = currentsize,
                list = data.tojson()
            };
        }

        
    }
}